from flask import Blueprint,jsonify
from db import db
import datetime
# from flask import current_app as app, request
from flask import request
user_blue = Blueprint('user',__name__)


@user_blue.route("/test")
def test():
    str = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')+"   "+request.path+"   error"
    app.logger.error(str)
    app.logger.info('info log')
    app.logger.warning('warning log')
    
    sql = "insert into user values(0,'aa22222','abc')"
    db.update(sql)
    db.commit()
    sql1 = "select * from user"
    res = db.find_all(sql1)
    print(res)
    return 'ok234234'


# 保险类型
@user_blue.route('/insurance_type', methods=['POST'])
def insurance_type():

    name = request.form.get('name')

    sql = 'insert into insurance_type (name) values ("%s")' % (name)
    db.update(sql)
    db.commit()

    return jsonify({
        'code': 200,
        'msg': '添加成功'
    })


# 保险
@user_blue.route('/insurance', methods=['POST'])
def insurance():

    name = request.form.get('name')
    insurance_id = request.form.get('insurance_id')

    sql = 'insert into insurance (name,insurance_id) values ("%s","%s")' % (name, insurance_id)
    db.update(sql)
    db.commit()
    return jsonify({
        'code': 200,
        'msg': '添加成功'
    })


# 用户
@user_blue.route("/user", methods=['POST'])
def user():
    username = request.form.get('username')
    password = request.form.get('password')
    name = request.form.get('name')
    insurance_id = request.form.get('insurance_id')

    if not username or not password or not name or not insurance_id:
        return jsonify({
            'code': 400,
            'msg': '数据不完善'
        })
    sql = 'insert into user(username, password,name,insurance_id) values ("%s","%s","%s","%s")' % (username, password,
                                                                                                   name, insurance_id)
    db.update(sql)
    db.commit()

    return jsonify({
        'code': 200,
        'msg': '登录成功'
    })
